﻿function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#avatar').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}
function readURL2(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#wallpaper').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$(document).ready(function () {
    $("#changeavatar").change(function () {
        readURL(this);
    });

    $("#changewallpaper").change(function () {
        readURL2(this);
    });

    $("#btn_edit_avatar").on("click", function () {
        var img = document.getElementById("changeavatar").value;
        if (img != "") {
            var file_data = document.getElementById('changeavatar').files[0];
            if (file_data == null) return; // chưa chọn file
            var form_data = new FormData();
            form_data.append("changeavatar", file_data);
            $.ajax({
                url: '../ajax/asp/user/upload_avatar.aspx',
                data: form_data,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function (data) {
                    //$("#divTreeView").html(data);
                }
            });
        }
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/user/edit_avatar.aspx?img=" + img, true);
        xhttp.send();
    });
    $("#btn_edit_wall").on("click", function () {
        var img = document.getElementById("changewallpaper").value;
        if (img != "") {
            var file_data = document.getElementById('changewallpaper').files[0];
            if (file_data == null) return; // chưa chọn file
            var form_data = new FormData();
            form_data.append("changewallpaper", file_data);
            $.ajax({
                url: '../ajax/asp/user/upload_banner.aspx',
                data: form_data,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function (data) {
                    //$("#divTreeView").html(data);
                }
            });
        }
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/user/edit_banner.aspx?img=" + img, true);
        xhttp.send();
    });

    $("#btn_edit_info").on("click", function () {
        var fullname = document.getElementById("personal_fullname").value;
        var email = document.getElementById("personal_email").value;
        var day = document.getElementById("personal_day").value;
        var month = document.getElementById("personal_month").value;
        var year = document.getElementById("personal_year").value;
        var phone = document.getElementById("personal_phone").value;
        var city = document.getElementById("personal_city").value;

        var birthday = year + "-" + month + "-" + day;
        var sex = "1";
        var error = 0;
        if ($('#user-female').is(":checked"))
            sex = "0";

        if (fullname.length == "" || fullname.length < 6) {
            $("#error_mes_name").removeClass("w3-hide");
            document.getElementById("error_mes_name").innerHTML = "Họ và tên quá ngắn!";
            error = 1;
        }
        else {
            $("#error_mes_name").addClass("w3-hide");
        }
        if (email.length == "") {
            $("#error_mes_email").removeClass("w3-hide");
            document.getElementById("error_mes_email").innerHTML = "Email không được để trống!";
            error = 1;
        }
        else {
            var check = /@/;
            if (check.test(email) == false) {
                $("#error_mes_email").removeClass("w3-hide");
                document.getElementById("error_mes_email").innerHTML = "Định dạng email không chính xác!";
                error = 1;
            }
            else {
                var temp = email.split('@');
                if (temp.length > 2) {
                    $("#error_mes_email").removeClass("w3-hide");
                    document.getElementById("error_mes_email").innerHTML = "Định dạng email không chính xác!";
                    error = 1;
                } else {
                    if (temp[1].toLowerCase() != "gmail.com" && temp[1].toLowerCase() != "yahoo.com" && temp[1].toLowerCase() != "yahoo.com.vn" && temp[1].toLowerCase() != "aol.com" && temp[1].toLowerCase() != "outlook.com" && temp[1].toLowerCase() != "senquocte.com") {
                        $("#error_mes_email").removeClass("w3-hide");
                        document.getElementById("error_mes_email").innerHTML = "Email @" + temp[1] + " không tồn tại hoặc không hỗ trợ!";
                        error = 1;
                    }
                    else {
                        $("#error_mes_email").addClass("w3-hide");
                    }
                }
            }
        }
        if (error == 0) {
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    location.reload(true);
                }
            };
            xhttp.open("GET", "../ajax/asp/user/edit_info.aspx?name=" + fullname + "&email=" + email + "&birthday=" + birthday + "&sex=" + sex + "&city=" + city + "&phone=" + phone, true);
            xhttp.send();
        }

    });

    $("#personal_month").on("change", function () {
        var day = document.getElementById("personal_day").value;
        var month = document.getElementById("personal_month").value;
        var year = document.getElementById("personal_year").value;
        if (month == 2) {
            if (day > 29) {
                if (year % 4 == 0)
                    document.getElementById("personal_day").value = 29;
                else
                    document.getElementById("personal_day").value = 28;
            }
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31) {
                    document.getElementById("personal_day").value = 30;
                }
            }
        }
    });

    $("#personal_day").on("change", function () {
        var day = document.getElementById("personal_day").value;
        var month = document.getElementById("personal_month").value;
        var year = document.getElementById("personal_year").value;

        if (month == 2 && day > 28) {
            if (year % 4 == 0)
                document.getElementById("personal_day").value = 29;
            else
                document.getElementById("personal_day").value = 28;
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31) {
                    document.getElementById("personal_day").value = 30;
                }
            }
        }
    });

    $("#personal_year").on("change", function () {
        var day = document.getElementById("personal_day").value;
        var month = document.getElementById("personal_month").value;
        var year = document.getElementById("personal_year").value;

        if (year % 4 == 0) {
            if (month == 2 && day > 29)
                document.getElementById("personal_day").value = 29;
        }
        else {
            if (month == 2 && day > 28)
                document.getElementById("personal_day").value = 28;
        }
    });
    $("#personal_province").on("change", function () {
        var id = document.getElementById("personal_province").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("personal_city").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/get_city.aspx?id=" + id, true);
        xhttp.send();
    });

    $("#btn_get_time").on("click", function () {
        var month = document.getElementById("hour-month").value;
        var year = document.getElementById("hour-year").value;
        var id = document.getElementById("id_staff").innerHTML;

        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("tbl_worked").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/time_worked.aspx?id=" + id + "&month=" + month + "&year=" + year, true);
        xhttp.send();
    });
    $(".work").dblclick(function () {
        var context = this.innerHTML;
        this.innerHTML = "<input class='edited' type='text' value = '" + context + "'/>";
    });

    $(".btn_edited_work").on("click", function () {
        if ($(".edited").length) {
            var week = document.getElementById("work-week").value;
            var row = document.getElementsByClassName("row_work");
            var edited = week + "_";
            for (var i = 0; i < row.length; i++) {
                if (row[i].getElementsByClassName("edited").length) {
                    edited += row[i].getElementsByClassName("row_id")[0].innerHTML;
                    edited += "_";
                    edited += row[i].getElementsByClassName("manager_id")[0].innerHTML;
                    edited += "_";
                    for (var j = 0; j < 8; j++) {
                        if (row[i].getElementsByClassName("work")[j].getElementsByClassName("edited").length) {
                            edited += row[i].getElementsByClassName("work")[j].getElementsByClassName("edited")[0].value;
                            edited += "|";
                        }
                        else
                            edited += "|";

                    }
                    edited += ",";
                }
            }
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    location.reload(true);
                }
            };
            xhttp.open("GET", "../ajax/asp/staff/working.aspx?edited=" + edited, true);
            xhttp.send();
        }
        else {
            alert("Lịch làm việc không thay đổi");
        }
    });

    $(".btn_add_working_form").on("click", function () {
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("lichLamViecMoi").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/add_working_form.aspx", true);
        xhttp.send();
    });

    $(".btn_add_working").on("click", function () {
        var row = document.getElementsByClassName("add_working");
        var add_work = "";
        for (var i = 0; i < row.length; i++) {
            add_work += row[i].getElementsByClassName("txt_manager_id")[0].innerHTML;
            add_work += "_";
            for (var j = 0; j < row[i].getElementsByClassName("add_work").length; j++) {
                add_work += row[i].getElementsByClassName("add_work")[j].value.trim();
                add_work += "|";
            }
            add_work += ",";
        }

        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/add_working.aspx?add_work=" + add_work, true);
        xhttp.send();
    });
    $("#work-year").on("change", function () {
        var year = document.getElementById("work-year").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("work-week").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/get_week.aspx?year=" + year, true);
        xhttp.send();
    });

    $("#btn_change_pass").on("click", function () {
        var newpass = document.getElementById("txt_newpass").value;
        var oldpass = document.getElementById("txt_oldpass").value;
        var repass = document.getElementById("txt_repass").value;

        if (newpass == "" || oldpass == "" || repass == "") {
            document.getElementById("messenge_pass").innerHTML = "Thông tin nhập vào chưa đầy đủ";
        } else {
            if (repass != newpass) {
                document.getElementById("messenge_pass").innerHTML = "Nhập lại mật khẩu không đúng";
            }
            else {
                if (newpass.length < 6) {
                    document.getElementById("messenge_pass").innerHTML = "Mật khẩu mới quá ngắn";
                }
                else {
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("change_pass").innerHTML = xhttp.responseText;
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/user/edit_pass.aspx?newpass=" + newpass + "&oldpass=" + oldpass, true);
                    xhttp.send();
                }
            }
        }
    });
});